package cn.com.xinjinfu.pdoulcProduct.manager;

import java.io.File;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import android.annotation.SuppressLint;
import android.content.Context;
import android.widget.EditText;

/**
 * 字符串操作工具包
 */
public class StringManager {
	private static DecimalFormat df;
	
	public static String getDecimalFormat(Object obj)
	{
		if(df == null){
			df = new DecimalFormat( "###,###,###,###,##0.##");
//			df.setMaximumFractionDigits(2);
//			df.setGroupingSize(0);
//			df.setRoundingMode(RoundingMode.FLOOR);
		}
		return df.format(obj);
	}
	public static String getDecimalFormat2(Object obj)
	{
			DecimalFormat df2 = new DecimalFormat( "##############0.00");
//			df.setMaximumFractionDigits(2);
//			df.setGroupingSize(0);
//			df.setRoundingMode(RoundingMode.FLOOR);
		return df2.format(obj);
	}
	public static String getDecimalFormat3(Object obj)
	{
		DecimalFormat df2 = new DecimalFormat( "##############0");
//			df.setMaximumFractionDigits(2);
//			df.setGroupingSize(0);
//			df.setRoundingMode(RoundingMode.FLOOR);
		return df2.format(obj);
	}
	public static String getDecimalFormat4(Object obj)
	{
		DecimalFormat df2 = new DecimalFormat( "###,###,###,###,##0.00");
//			df.setMaximumFractionDigits(2);
//			df.setGroupingSize(0);
//			df.setRoundingMode(RoundingMode.FLOOR);
		return df2.format(obj);
	}
	/**
	 * 验证邮箱地址是否正确
	 * 
	 * @param email
	 * @return
	 */
	public static boolean checkEmail(String email) {
		boolean flag = false;
		try {  
			String check = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
			Pattern regex = Pattern.compile(check);
			Matcher matcher = regex.matcher(email);
			flag = matcher.matches();
		} catch (Exception e) { 
			flag = false;
		}

		return flag;
	}
	/**
	 * 验证手机号码
	 * 
	 * @param mobiles
	 * @return
	 */
	public static boolean isMobileNO(String mobiles) {
		boolean flag = false;
		try {
			Pattern p = Pattern
//					.compile("^[1][3-8]+\\d{9}");
			.compile("^((13[0-9])|(15[^4,\\D])|(18[0,0-9]))\\d{8}$");
//			.compile("^[1]([3][0-9]{1}|59|58|88|89)[0-9]{8}$");
			Matcher m = p.matcher(mobiles);
			flag = m.matches();
		} catch (Exception e) { 
			flag = false;
		}
		return flag;
	}
	/**
	 * 判断给定字符串是否空白串。 空白串是指由空格\s、制表符\t 、回车符\n、换行符\r 组成的字符串
	 * 若输入字符串为null或空字符串，返回true
	 * @param input
	 * @return boolean
	 */
	public static boolean isEmpty(String input) {
		return input == null || "".equals(input);
	}
	/**
	 * 判断是否手机号码
	 * 
	 * @param cellphone
	 * @return
	 */
	public static boolean isBadCellphone(String cellphone) {
		return cellphone.length() != 11;
	}

	/**
	 * 判断密码的格式
	 * 
	 * @param pwd
	 * @return
	 */
	public static boolean isBadPwd(String pwd) {
		return false;// 密码没有做最少位数限制
	}

	public static boolean isBadVerificationCode(String code) {
		return false;
	}
	
	/**
	 * 解析Stirng获取int
	 * @param v
	 * @return
	 */
	public static int parseInt(String v){
		if(v==null)return 0;
		double d = Double.valueOf(v);
		return (int)d;
	}
	
	/**
	 * 解析Stirng获取double
	 * @param v
	 * @return
	 */
	public static double parseDouble(String v){
		if(v==null)return 0.00;
		return Double.valueOf(v);
	}
	
	public static String parseDouble(double v){
		return getDecimalFormat(v);
	}
	public static String parseDouble2(double v){
		return getDecimalFormat2(v);
	}
	public static String parseDouble3(double v){
		return getDecimalFormat3(v);
	}
	public static String parseDouble4(double v){
		return getDecimalFormat4(v);
	}
	
	/**
	 * 解析金额
	 * @param v
	 * @return
	 */
	public static String parseAmount(String v){
		if(v == null)return "0";
		double d = Double.parseDouble(v);
		/*if(d>1000000){
			return df.format(d /10000) + "万";
		}*/
		return getDecimalFormat(d);
//		return v+"";
	}
	
	/**
	 * 解析金额
	 * @param v
	 * @return
	 */
	public static String parseAmount(long v){
		if(v>=100000000){
			return (int)(v *0.00000001) + "亿";
		}else if(v>=1000000){
			return (int)(v / 10000) + "万";
		}
		return v+"";
	}
	
	/**
	 * 解析金额
	 * @param v
	 * @return
	 */
	public static String parseAmount(float v){
		if(v>=100000000){
			return getDecimalFormat(v *0.00000001) + "亿";
		}else if(v>=1000000){
			return getDecimalFormat(v / 10000) + "万";
		}
		return getDecimalFormat(v)+"";
	}
	
	/**
	 * 解析金额
	 * @param v
	 * @return
	 */
	public static String parseAmount(double v){
		if(v>=100000000){
			return getDecimalFormat(v *0.00000001) + "亿";
		}else if(v>=100){
			return getDecimalFormat(v / 10000) + "";
		}
		return getDecimalFormat(v);
	}


	/**
	 * 将EditText中中文本获取到
	 */
	public static String getStringByET(EditText et) {
		return et.getText().toString().trim();
	}

	/**
	 * 像素和sp 相互转换
	 */
	public static int spToPix(Context context, int sp) {
		final float scale = context.getResources().getDisplayMetrics().density;
		return (int) (sp * scale + 0.5f);
	}

	/**
	 * 根据文件绝对路径获取文件名
	 * 
	 * @param filePath
	 * @return
	 */
	public static String getFileName(String filePath) {
		if (isEmpty(filePath))
			return "";
		return filePath.substring(filePath.lastIndexOf(File.separator) + 1);
	}

	/**
	 * 去除字符串中的空格、回车、换行符、制表符
	 * 
	 * @param str
	 * @return
	 */
	public static String replaceBlank(String str) {
		String dest = "";
		if (str != null) {
			Pattern p = Pattern.compile("\r|\n");
			Matcher m = p.matcher(str);
			dest = m.replaceAll("");
		}
		return dest;
	}

	/**
	 * 删除Html标签
	 * 
	 * @param inputString
	 * @return
	 */
	public static String htmlRemoveTag(String inputString) {
		if (inputString == null)
			return null;
		String htmlStr = inputString; // 含html标签的字符串
		String textStr = "";
		Pattern p_script;
		Matcher m_script;
		Pattern p_style;
		Matcher m_style;
		Pattern p_html;
		Matcher m_html;
		try {
			// 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
			String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
			// 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
			String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
			String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
			p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
			m_script = p_script.matcher(htmlStr);
			htmlStr = m_script.replaceAll(""); // 过滤script标签
			p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
			m_style = p_style.matcher(htmlStr);
			htmlStr = m_style.replaceAll(""); // 过滤style标签
			p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
			m_html = p_html.matcher(htmlStr);
			htmlStr = m_html.replaceAll(""); // 过滤html标签
			textStr = htmlStr;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return textStr;// 返回文本字符串
	}
	@SuppressLint("SimpleDateFormat")
	public static String getTime(long time) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		return format.format(new Date(time));
	}
	
	/**
	 * 手机号加*
	 */
	public static String getPhoStar(String phone){
		if(phone==null || phone.length()<11)
			return phone;
		char[]  chs = phone.toCharArray();
		for(int i=3;i<7;i++)
			chs[i] = '*';
		return String.valueOf(chs); 
	}
	/**
	 * 邮箱加*
	 */
	public static String getEmailStar(String email){
		int index;  
		if(email==null || (index = email.indexOf("@"))<4)
			return email;
		int len = Math.min(index/2, 4);
		char[]  chs = email.toCharArray();
		for(int i=index-len-1;i<index-1;i++)
			chs[i] = '*';
		return String.valueOf(chs); 
	}
}
